from util.ParseConfiurationFile import ParseConfigFile
from util.ObjectMap import *

class AddressBookPage:
    def __init__(self,driver):
        self.driver = driver
        self.cf = ParseConfigFile()
        self.addContactsOptions = self.cf.getItemSection("126mail_addContactsPage")
        print(self.addContactsOptions)

    def createContactPersonButton(self):
        try:
            locateType, locatorExpression = self.addContactsOptions["addContactsPage.createContactsBtn".lower()].split(">")
            elementObj = getElement(self.driver,locateType,locatorExpression)
            return elementObj
        except Exception as e:
            raise e

    def contactPersonName(self):
        try:
            locateType, locatorExpression = self.addContactsOptions["addContactsPage.addName".lower()].split(">")
            elementObj = getElement(self.driver,locateType,locatorExpression)
            return elementObj
        except Exception as e:
            raise e


    def contactEmail(self):
        try:
            locateType, locatorExpression = self.addContactsOptions["addContactsPage.addEmail".lower()].split(">")
            elementObj = getElement(self.driver, locateType, locatorExpression)
            return elementObj
        except Exception as e:
            raise e

    def contactPhone(self):
        try:
            locateType, locatorExpression = self.addContactsOptions["addContactsPage.addPhone".lower()].split(">")
            elementObj = getElement(self.driver, locateType, locatorExpression)
            return elementObj
        except Exception as e:
            raise e

    def contactRelation(self):
        try:
            locateType, locatorExpression = self.addContactsOptions["addContactsPage.addrealtion".lower()].split(">")
            elementObj = getElement(self.driver, locateType, locatorExpression)
            return elementObj
        except Exception as e:
            raise e

    def contactSave(self):
        try:
            locateType, locatorExpression = self.addContactsOptions["addContactsPage.saveButton".lower()].split(">")
            elementObj = getElement(self.driver, locateType, locatorExpression)
            return elementObj
        except Exception as e:
            raise e
if __name__ == '__main__':
    from selenium import webdriver
    import time
    from appModules.LoginAction import LoginAction
    from pageObjects.HomePage import HomePage

    driver = webdriver.Chrome()
    driver.get("http://mail.126.com/")
    LoginAction.login(driver, "wyu0430", "19870303")
    homepage = HomePage(driver)
    homepage.addressLink().click()
    addpage = AddressBookPage(driver)
    addpage.createContactPersonButton().click()
    time.sleep(5)
    addpage.contactPersonName().send_keys("test")
    addpage.contactEmail().send_keys("test@163.com")
    addpage.contactPhone().send_keys("123456")
    addpage.contactRelation().send_keys("朋友")
    addpage.contactSave().click()
    time.sleep(5)
    driver.quit()

